<?php

class I18n_Message
{
	protected $_lang = null;

	protected $_data = null;
	
	public function __construct($lang)
	{
		$this -> _lang = $lang;
	}

	/**
	 * @param string $message_id
	 * @param bool $plural optional default false
	 * @return void
	 */
	public function formatMessage($message, $values = NULL, $plural = FALSE)
	{
		if (null === $this -> _data)
		{
			$key =  'core/message/' . $this -> _lang;

			$cache = Cache::instance();

			if (FALSE == ($this -> _data = $cache -> get($key)))
			{
				$adapter = new I18n_Message_Db;
				$this -> _data = $adapter -> load($this -> _lang);
				$cache->set($key, $this->_data);
			}
		}

		$message = (string)$message;

		if (isset($this -> _data[$message]) == true)
		{
			$message = $this -> _data[$message];
		}

		if (NULL != $values)
		{
			return strtr($message, $values);
		}
		return $message;
	}
}
